<?php
class InfomationAction extends BaseAction {
	public function newsList() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$News= M('News'); 
		
		$condition['type'] = '1';
		//$condition['sort'] = '健康新闻'; 
		$list = $News->where($condition)->select();
		$count = count($list);
    	// 导入分页类
   	 	import("ORG.Util.Page");
    	// 实例化分页类
    	$p = new Page($count, 12);
    	$p->setConfig('prev',"<");  
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('next','>');
    	
    	// 分页显示输出
    	$page = $p->show();
		$list = $News->order('newsId DESC')->limit($p->firstRow.','.$p->listRows)->where($condition)->select();
		
		for($i=0;$i<$count;$i++) {
			if($list[$i]["state"]==0) $list[$i]["state_cn"]="不可用";
			else $list[$i]["state_cn"]="可用";
		}
		
		$this->assign('page', $page);
		$this->assign('newslist',$list);
		$this->assign('navi',"健康新闻");
		$this->assign('title',"健康新闻列表");
		$this->display();
	}
	
	public function newsArticle() {
		//防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$News = M('News');
		$condition['newsId']=$_GET['id'];
		$info = $News->where($condition)->find();
		
		if($_POST['title']) {
			if(is_uploaded_file($_FILES['image']['tmp_name'])){
				$upfile=$_FILES["image"];
				$name=$upfile["name"]; //重命名照片
				$tmp=strtolower(substr($name, -4,4));
				$type=$upfile["type"];
				$size=$upfile["size"];
				$tmp_name=$upfile["tmp_name"];
				$error=$upfile["error"];
				$name=date('YmdHis').$tmp;
				if($error=='0') move_uploaded_file($tmp_name,'Public/News/Picture/'.$name);
				if(is_file('Public/News/Picture/'.$info['attachment'])) 
					unlink('Public/News/Picture/'.$info['attachment']);		
 			}
			if($info['title']!=$_POST['title']) $data['title'] = $_POST['title'];
			if($info['sort']!=$_POST['type']) $data['sort'] = $_POST['type'];
			if($info['createTime']!=$_POST['ann_time']) $data['createTime'] = $_POST['ann_time'];
			if($info['state']!=$_POST['state']) $data['state'] = $_POST['state'];
			if($info['summary']!=$_POST['summary']) $data['summary'] = $_POST['summary'];
			if($info['content']!=$_POST['content']) $data['content'] = $_POST['content'];
			if(!empty($name)) $data['attachment'] = $name;
			
			if($data) {
				$data['author'] = $_SESSION['admin']['admName'];
				$data['modifyTime'] = date('Y-m-d H:i:s',time());
				$result = $News->where($condition)->save($data);
				if($result)
					echo "<script language=\"javascript\">alert('修改成功！');window.location.href='newsArticle/id/".$_GET['id']."';</script>";
				else 
					$this->error ( '数据写入错误！请重试...' );
			} else 
				echo "<script language=\"javascript\">alert('资料没有更新！');window.location.href='newsArticle/id/".$_GET['id']."';</script>";
		} 
		$this->assign('news',$info);
		$this->assign('navi',"健康新闻");
		$this->assign('title',"健康新闻内容列表");
		$this->display();
	}
	
	public function addNews() {
		//防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		
		if($_POST['title']) {
			$News = M('News');
			
			if(is_uploaded_file($_FILES['image']['tmp_name'])){
				$upfile=$_FILES["image"];
				$name=$upfile["name"]; //重命名照片
				$tmp=strtolower(substr($name, -4,4));
				$type=$upfile["type"];
				$size=$upfile["size"];
				$tmp_name=$upfile["tmp_name"];
				$error=$upfile["error"];
				$name=date('YmdHis').$tmp;
				if($error=='0') move_uploaded_file($tmp_name,'Public/News/Picture/'.$name);		
 			}
 			
 			$data['title']=$_POST['title'];
			$data['sort']=$_POST['type'];
			$data['createTime']=$_POST['ann_time'];
			$data['summary'] = $_POST['summary'];
			if(!empty($name)) $data['attachment']=$name;
			$data['state']=$_POST['state'];
			$data['content']=$_POST['content'];
			$data['type'] = '1';
			$data['author'] = $_SESSION['admin']['admName'];
			$data['modifyTime'] = date('Y-m-d H:i:s',time());
			
			$result = $News->add($data);
			if($result)
				echo "<script language=\"javascript\">alert('添加成功！');window.location.href='newsList';</script>";
			else 
				$this->error ( '数据写入错误！请重试...' );
		} else {
			$this->assign('navi',"新闻添加");
			$this->assign('title',"添加健康新闻");
			$this->display();
		}
	}
	
	public function deleteNews() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		$News = M('News');
		$condition['newsId']=$_GET['id'];
		$info = $News->where($condition)->find();
		if(is_file('Public/News/Picture/'.$info['attachment'])) 
			unlink('Public/News/Picture/'.$info['attachment']);	
		$News->where($condition)->delete();
		$this->redirect('newsList');
	}
	
	public function moduleList() {
		// 防止页面乱码  
		header ( 'Content-type:text/html;charset=utf-8' );
		
		$Module= M('Newsmodule'); 
		
		$condition['state'] = '1';
		//$condition['sort'] = '健康新闻'; 
		$list = $Module->where($condition)->select();
		$count = count($list);
    	// 导入分页类
   	 	import("ORG.Util.Page");
    	// 实例化分页类
    	$p = new Page($count, 12);
    	$p->setConfig('prev',"<");  
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('next','>');
    	
    	// 分页显示输出
    	$page = $p->show();
		$list = $Module->order('moduleId')->limit($p->firstRow.','.$p->listRows)->where($condition)->select();
		
		for($i=0;$i<$count;$i++) {
			if($list[$i]["state"]==0) $list[$i]["state_cn"]="不可用";
			else $list[$i]["state_cn"]="可用";
		}
		
		$this->assign('page', $page);
		$this->assign('modulelist',$list);
		$this->display();
	}
}